using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Share.Controls;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.DOWNLOAD.Controls
{
    public partial class ItemList : ModulePage
    {
        protected string GetAddEditItemUrl(int itemID)
        {            
            Hashtable settings = this.Settings;
            if (settings["InputOption"].ToString().Equals("Simple"))
            {
                return HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, itemID, "SimpleAddEditItem", "");
            }
            return HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, itemID, "AddEditItem", "");
        }

        protected string GetImageUrl(object url)
        {
            if (url == null)
            {
                return "~/Images/NoImage.gif";
            }
            if (url.ToString().Length == 0)
            {
                return "~/Images/NoImage.gif";
            }
            return url.ToString();
        }

        private void BuildPageLink()
        {
            if (Request.QueryString["CategoryID"] != null)
            {
                int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");
                Category category = Category.Load(categoryID);
                if (category != null)
                {
                    Category parentCategory = Category.Load(category.ParentID);
                    if (parentCategory != null)
                    {
                        pagelink.AddLink(parentCategory.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemList, "categoryid={0}", parentCategory.ID)));

                        string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemList, "categoryid={0}", category.ID));
                        pagelink.AddLink(category.Title, url);
                    }
                    else
                    {
                        pagelink.AddLink(category.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.ItemList, "categoryid={0}", category.ID)));
                    }
                }
            }
        }

        private void GridDataBinding(int pageSize, int pageIndex)
        {
            DataView dv;
            if (Request.QueryString["CategoryID"] != null)
            {
                dv = Components.Item.GetItemsByCategoryID(Int32.Parse(Request.QueryString["categoryid"]), pageSize, pageIndex).Tables[0].DefaultView;
            }
            else
            {
                if (Request.QueryString["OptionView"] != null)
                {
                    dv = Components.Item.GetTopItems(this.ModuleID, Request.QueryString["OptionView"], pageSize, pageIndex).Tables[0].DefaultView;
                }
                else
                {
                    dv = Components.Item.GetTopItems(this.ModuleID, string.Empty, pageSize, pageIndex).Tables[0].DefaultView;
                }
            }
            gvItem.DataSource = dv;
            gvItem.DataBind();

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Hashtable settings = this.ParentControl.Settings;
                int pageSize = Convert.ToInt32(settings["PageSize"].ToString());
                int categoryID = 0;
                if (Request.QueryString["CategoryID"] != null)
                {
                    categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                }

                int itemCount = Components.Item.GetItemCount(this.ModuleID, categoryID);
                int pageCount = itemCount / pageSize;

                
                if (itemCount % pageSize > 0) pageCount = pageCount + 1;
                if (pageCount == 0) pageCount = 1;
                if (itemCount > 0)
                {
                    TotalRecordLabel.Text = itemCount.ToString("N0");
                    for (int i = 1; i <= pageCount; i++)
                    {
                        PageDropDownList.Items.Add(i.ToString());
                    }
                    PageLabel.Text = " / " + pageCount.ToString("N0") + " trang";
                }

                this.BuildPageLink();
                this.GridDataBinding(pageSize, 1);

                PageDropDownList.Attributes.Add("onchange", "javascript:window.scrollTo(0,0)");
            }
        }

        protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Hashtable settings = this.ParentControl.Settings;
            int pageSize = Convert.ToInt32(settings["PageSize"].ToString());

            DropDownList ddl = (DropDownList)sender;
            this.GridDataBinding(pageSize, Convert.ToInt32(ddl.SelectedIndex) + 1);
        }
    }
}